package com.uspacex.calculate.controller;

import com.alibaba.fastjson.JSONObject;
import com.uspacex.calculate.entity.MemberTrace;
import com.uspacex.calculate.mapper.DisplayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/display")
public class DisplayController {

    @Autowired
    DisplayMapper displayMapper;

    @RequestMapping(value = "/enterprises", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    @ResponseBody
    Map<String, Object> getEnterpriseList() {
        Map<String, Object> map = new HashMap<>();
        map.put("data", displayMapper.enterpriseList());
        return map;
    }

    @RequestMapping(value = "/enterprise/basic-info", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    Map<String, Object> getEnterpriseBasicInfo(@RequestBody String str) {
        Map<String, Object> resquestParams = JSONObject.parseObject(str, Map.class);
        Map<String, Object> map = new HashMap<>();

        //企业基本信息
        map.put("basicInfo", displayMapper.businessLicense((String) resquestParams.get("name")));
        map.put("basicInfoComments", displayMapper.selectComment("db_enterprise_info", "business_license"));
        map.put("enterprisePublicOpinion", displayMapper.enterprisePublicOpinion((String) resquestParams.get("name")));
        map.put("enterprisePublicOpinionCount", displayMapper.enterprisePublicOpinionCount((String) resquestParams.get("name")));
        map.put("justiceInformation", displayMapper.justiceInformation((String) resquestParams.get("name")));
        map.put("justiceInformationCount", displayMapper.justiceInformationCount((String) resquestParams.get("name")));
        map.put("intellectualProperty", displayMapper.intellectualProperty((String) resquestParams.get("name")));
        map.put("intellectualPropertyCount", displayMapper.intellectualPropertyCount((String) resquestParams.get("name")));
        map.put("alteration", displayMapper.alteration((String) resquestParams.get("name")));
        map.put("alterationCount", displayMapper.alterationCount((String) resquestParams.get("name")));
        map.put("balanceSheet", displayMapper.balanceSheet((String) resquestParams.get("name")));
        map.put("cashFlow", displayMapper.cashFlow((String) resquestParams.get("name")));
        map.put("profit", displayMapper.profit((String) resquestParams.get("name")));
        map.put("memberTrace", displayMapper.memberTrace((String) resquestParams.get("name")));


        return map;
    }


    /**
     * 每人每季度共30次位置信息，在设定经纬度范围内，随机生成10个位置，依次选择位置放入新数组，为每个位置生成随机数（次数），次数累加达到九十停止
     * @return
     */
    @RequestMapping(value = "/test/insert", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    @ResponseBody
    Map<String, Object> insertData() {
        Map<String, Object> map = new HashMap<>();

        String eName = "山东新华医疗器械股份有限公司";
        List<LinkedHashMap<String, Object>> memberList = displayMapper.member(eName);

        float longitude = 118.05309f;
        float latitude = 36.846855f;

        String [] dateArr = {"2015/6/30", "2015/9/30", "2015/12/31", "2016/3/31", "2016/6/30", "2016/9/30",
                "2016/12/31", "2017/3/31"};

        for(String date : dateArr) {
            for (LinkedHashMap member : memberList) {
                MemberTrace memberTrace = new MemberTrace();
                memberTrace.setEnterpriseName(eName);
                memberTrace.setDateInfo(date);
                memberTrace.setPosition((String) member.get("position"));
                memberTrace.setMemberName((String) member.get("name"));

                int timesCount = 0;
                while (timesCount < 45) {
                    float newLongitude = longitude + (float) Math.random()*(Math.random()>0.5?1:-1);
                    float newLatitude = latitude + (float) Math.random()*(Math.random()>0.5?1:-1);
                    memberTrace.setLongitude(newLongitude);
                    memberTrace.setLatitude(newLatitude);

                    int times = 3 + (int)Math.floor(Math.random()*7);

                    if (45 - timesCount < 4) {
                        times = 45 - timesCount;
                    }

                    timesCount += times;

                    memberTrace.setTimes(times);
                    displayMapper.insertTrace(memberTrace);
                }

            }
        }


        map.put("message", "success");
        return map;
    }

}
